package cn.iocoder.mall.resource.controller;

import cn.iocoder.common.utils.common.api.vo.Result;
import cn.iocoder.mall.resource.utils.MinIOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/***
 *@author yinxucun
 *@date 2019-11-25 13:43 
 */
@RestController
@RequestMapping("/resource/image")
public class ImageController  {

    @Autowired
    private MinIOUtils minIOUtils;

    @PostMapping("/uploadImage")
    public Result<?> uploadImage(@RequestParam("file") MultipartFile file){
        return minIOUtils.uploadImage(file);

    }

    @GetMapping("/getImageByUri")
    public void   getImageByUri(@RequestParam("imageUri")String imageUri, HttpServletRequest request, HttpServletResponse response) throws Exception {

        response.sendRedirect(minIOUtils.getImageByUri(imageUri));
    }

    @GetMapping("/getImageStringByUri")
    public Result   getImageStringByUri(@RequestParam("imageUri")String imageUri, HttpServletRequest request, HttpServletResponse response) throws Exception {

        return Result.ok(minIOUtils.getImageByUri(imageUri));
    }

    @PostMapping("/showImageList")
    public Result showImageList(@RequestBody Map<String,Object> params) throws Exception {


        return Result.ok(minIOUtils.getUriList((List<String>) params.get("list")));

    }
}
